<template>
  <page-header-wrapper>
    <div>
    <a-row :gutter="24">
      <a-col :span="4">
        <a-card :bordered="false" class="ant-pro-components-tag-select" style="height: 800px">
          <a-input-search placeholder="请输入内容" @search="onSearch" />

          <a-tree :show-line="true"
                  :expanded-keys="expandedKeys"
                  :auto-expand-parent="autoExpandParent"
                  :tree-data="treedataWhole"
                  @expand="onExpand"
                  @select="selecttree"
          >
          </a-tree>
        </a-card>
      </a-col>
      <a-col :span="20">
        <a-card :bordered="false">
          <div class="table-page-search-wrapper">
            <a-form layout="inline" :label-col="labelCol" :wrapper-col="wrapperCol" labelAlign="left" :form="form" >
              <a-row :gutter="48">

                <template v-if="!advanced">
                <a-col :md="8" :sm="24">
                  <a-form-item label="模糊搜索">
                    <a-input  placeholder="" v-model="search" @change="changescale"/>
                  </a-form-item>
                </a-col>
                </template>
                <template v-if="advanced">
                  <a-col :md="8" :sm="24">
                    <a-form-item label="IP地址:">
                      <a-input placeholder="192.168.1.1-192.168.1.254" v-decorator="['ip']" @change="changescale"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="资产ID:">
                      <a-input placeholder="请输入" v-decorator="['id']" @change="changescale"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="资产所属人:">
                      <a-input placeholder="请输入" v-decorator="['owner']" @change="changescale"/>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="操作系统:">
                      <a-select  placeholder="请选择" v-decorator="['os']" @change="changescale">
                        <a-select-option value="">全部</a-select-option>
                        <a-select-option v-for="item in SystermType" :value="item.value">{{item.label}}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="安全等级:">
                      <a-select  placeholder="请选择" v-decorator="['safe']" @change="changescale">
                        <a-select-option value="">全部</a-select-option>
                        <a-select-option v-for="item in patchLevel" :value="item.value">{{item.label}}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="测试标记 :">
                      <a-select  placeholder="请选择" v-decorator="['isTest']" @change="changescale">
                        <a-select-option value="">全部</a-select-option>
                        <a-select-option v-for="item in TestMark" :value="item.value">{{item.label}}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="可用标记 :">
                      <a-select  placeholder="请选择" v-decorator="['isAvailable']" @change="changescale">
                        <a-select-option value="">全部</a-select-option>
                        <a-select-option v-for="item in AvalilableMark" :value="item.value">{{item.label}}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="发现时间 :">
                      <a-date-picker  v-decorator="['disTime']" style="width: 100%;" @change="changescale"/>
                    </a-form-item>
                  </a-col>
                </template>
                <a-col :md="(!advanced && 8) || 24" :sm="24">
                  <span
                    class="table-page-search-submitButtons"
                    :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
                  >
                    <a-button type="primary" @click="seachlist()">查询</a-button>
                    <a-button style="margin-left: 8px" @click="resetform()">重置</a-button>

                    <a @click="toggleAdvanced" style="margin-left: 8px">
                      {{ advanced ? '收起' : '展开' }}
                      <a-icon :type="advanced ? 'up' : 'down'" />
                    </a>
                  </span>
                </a-col>
              </a-row>
              <a-row style="margin-top: 20px;margin-bottom: 20px;">
                <a-button type="primary" @click="createnew()">新建</a-button>
                <a-button type="primary" @click="modifyassets()" style="margin-left: 10px">修改</a-button>
                <a-button type="primary" @click="deleteassets()" style="margin-left: 10px">删除</a-button>
                <a-button type="primary" @click="setdepartment()" style="margin-left: 10px">设置组织</a-button>
                <a-button type="primary" @click="configassets()" style="margin-left: 10px">配置</a-button>
                <a-button type="primary" @click="settest()" style="margin-left: 10px" se>设置测试</a-button>
                <a-button type="primary" @click="hebing()" style="margin-left: 10px">资产合并</a-button>
                <a-button type="primary" @click="checkable()" style="margin-left: 10px">可用性探测</a-button>
                <a-button  :loading="safecheckloading" type="primary" @click="checksafe()" style="margin-left: 10px">安全检查</a-button>
                <a-button type="primary" @click="autocheckrule()" style="margin-left: 10px">自动安检规则</a-button>
                <a-button disabled type="primary" style="margin-left: 10px" >回滚</a-button>
                <a-button @click="exportexcel()" type="primary" style="margin-left: 10px">导出Excel</a-button>
                <a-upload
                  name="file"
                  action="/api/back/excel/importInventorylist"
                  @change="getassetslist"
                  method="post"
                  accept=".xls,.xlsx"
                >
                  <a-button type="primary" style="margin-left: 10px">导入Excel</a-button>
                </a-upload>
              </a-row>
            </a-form>
          </div>

          <a-row>
            <a-table :columns="columns" :data-source="data" :row-selection="rowSelection" :pagination="tablepagination" :scroll="{ x: 1500}" @change="tablechange">
              <template slot="safeLv" slot-scope="text, record">
                <a-tag v-if="text == -1" color="gray">{{patchLevelMap[text.toString()]}}</a-tag>
                <a-tag v-if="text == 0" color="green">{{patchLevelMap[text.toString()]}}</a-tag>
                <a-tag v-if="text == 1" color="blue">{{patchLevelMap[text.toString()]}}</a-tag>
                <a-tag v-if="text == 2" color="orange">{{patchLevelMap[text.toString()]}}</a-tag>
                <a-tag v-if="text == 3" color="red">{{patchLevelMap[text.toString()]}}</a-tag>
              </template>
              <template slot="unpatchNum" slot-scope="text, record">
                <a>{{text}}</a>
              </template>
              <template slot="lastUnpatchNum" slot-scope="text, record">
                <a>{{text}}</a>
              </template>

              <template slot="disTime" slot-scope="text, record">
                {{text.replace('T',' ')}}
              </template>
              <template slot="isTest" slot-scope="text, record">
                <a-tag v-if="text == 0" color="red">{{TestMarkMap[text]}}</a-tag>
                <a-tag v-if="text == 1" color="green">{{TestMarkMap[text]}}</a-tag>
              </template>
              <template slot="isAvailable" slot-scope="text, record">
                <a-tag v-if="text == -1" color="#f5222d">{{AvalilableMarkMap[text]}}</a-tag>
                <a-tag v-if="text == 0" color="#bbbbbb">{{AvalilableMarkMap[text]}}</a-tag>
                <a-tag v-if="text == 1" color="#52c41a">{{AvalilableMarkMap[text]}}</a-tag>
                <a-tag v-if="text == 2" color="#1890ff">{{AvalilableMarkMap[text]}}</a-tag>
              </template>

              <template slot="ld" slot-scope="text, record">
                <a-button type="link" @click="gethole(record)">
                  详情
                </a-button>
              </template>
              <template slot="pack" slot-scope="text, record">
                <a-button type="link" @click="getpack(record)">
                  详情
                </a-button>
              </template>
            </a-table>
          </a-row>
        </a-card>
      </a-col>
    </a-row>

    </div>

    <Mymodal
      ref="mymodal"
    ></Mymodal>
    <Detailmodal ref="detailmodal"></Detailmodal>
  </page-header-wrapper>
</template>
<script>
import Mymodal from '@/components/mycomp/mymodal.vue'
import {getDepartmentList} from '@/api/department'
import {getAssetsinfolist,addassets,deleteassets,setassetsdepartment,setassetsTest,getpackinfo,getholeinfo,mergeassets} from '@/api/assets'
import {
  SystermType,
  patchLevel,
  TestMark,
  AvalilableMark,
  patchLevelMap,
  AvalilableMarkMap,
  TestMarkMap,
  TrueFalse,
  DangerLevelMap, OstypeMap, PatchTypeMap
} from '../../utils/constant'
import Detailmodal from '@/components/mycomp/detailmodal'

export default {
  name: 'AssetInfo',
  components: { Mymodal,Detailmodal },
  data() {
    return {
      safecheckloading:false,
      SystermType,
      patchLevel,
      TestMark,
      AvalilableMark,
      patchLevelMap,
      AvalilableMarkMap,
      TestMarkMap,
      search:'',
      form:this.$form.createForm(this, { name: 'assestinfosearch' }),
      treedata:[],
      treedataWhole:[],
      expandedKeys:[],
      autoExpandParent:true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      records: 922,
      columns:[
        {
          title: '资产ID',
          dataIndex: 'id',
          key: 'id',
          width:100,
          fixed: 'left'
        },
        {
          title: '资产名称',
          dataIndex: 'name',
          key: 'name',
          fixed: 'left',
          width:100,
        },
        {
          title: '主机名',
          dataIndex: 'hostname',
          key: 'hostname',
          width:100,
        },
        {
          title: '资产所属部门',
          dataIndex: 'departmentName',
          key: 'departmentName',
          width:150,
        },
        {
          title: 'IP地址',
          dataIndex: 'ip',
          key: 'ip',
          width:150,
        },
        {
          title: 'MAC地址',
          dataIndex: 'mac',
          key: 'mac',
          width:150,
        },
        {
          title: '操作系统',
          dataIndex: 'os',
          key: 'os',
          width:150,
        },
        {
          title: '安全等级',
          dataIndex: 'safeLv',
          key: 'safeLv',
        //  sorter: (a, b) => a - b,
          width:150,
          scopedSlots: { customRender: 'safeLv' },
        },
        {
          title: '未打补丁数',
          dataIndex: 'unpatchNum',
          key: 'unpatchNum',
          //sorter: (a, b) => a - b,
          width:150,
          scopedSlots: { customRender: 'unpatchNum' },
        },
        {
          title: '上次扫描未打补丁数',
          dataIndex: 'lastUnpatchNum',
          key: 'lastUnpatchNum',
          //sorter: (a, b) => a - b,
          width:150,
          scopedSlots: { customRender: 'lastUnpatchNum' },
        },


        {
          title: '资产所属人',
          dataIndex: 'owner',
          key: 'owner',
          width:150,
        },
        {
          title: '发现时间',
          dataIndex: 'disTime',
          key: 'disTime',
          width:200,
          scopedSlots: { customRender: 'disTime' },
        },
        {
          title: '测试标记',
          dataIndex: 'isTest',
          key: 'isTest',
          width:100,
          scopedSlots: { customRender: 'isTest' },
        },
        {
          title: '可用标记',
          dataIndex: 'isAvailable',
          key: 'isAvailable',
          width:100,
          scopedSlots: { customRender: 'isAvailable' },
        },
        {
          title: '漏洞',
          dataIndex: 'ld',
          key: 'ld',
          width:100,
          fixed: 'right',
          scopedSlots: { customRender: 'ld' },
        },
        {
          title: '已安装包',
          dataIndex: 'pack',
          key: 'pack',
          width:100,
          fixed: 'right',
          scopedSlots: { customRender: 'pack' },
        },
      ],
      rowSelection:{
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectarray = selectedRows
          this.changescale()
        },
        onSelect: (record, selected, selectedRows) => {
        },
        onSelectAll: (selected, selectedRows, changeRows) => {
        },
      },
      tablepagination:{
        showTotal:(total, range) => {return `共搜索到${total}条数据`},
        current:1,
        total:0
      },
      data:[],
      groupId:null,
      selectarray:null
    }
  },
  mounted() {
    this.loaddepartment()
    this.getassetslist()


  },
  methods: {
    seachlist(){
      this.tablepagination.current=1;
      this.getassetslist()
    },
    tablechange(pagination){
      this.tablepagination = pagination
      this.getassetslist()
    },
    configassets(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请选择一条数据')
        return
      }
      if(this.selectarray && this.selectarray.length >1){
        this.$message.info('此操作只支持一条数据')
        return
      }
      let id = this.getids()
      let hosts = []
      for(let i in this.selectarray){
        hosts.push(this.selectarray[i].name)
      }
      let data = hosts.join(', ')
      let modalinfo = {
        title:'配置',
        list:[
          {name:'port',label:'端口',require:true,type:'number',errortip:'请输入端口'},
          {name:'username',label:'用户名',require:true,type:'input',errortip:'请选择用户名'},
          {name:'password',label:'密码',type:'password',require:true,errortip:'请输入密码'},
        ],
        ok:this.configokfun.bind(this),
        moreinfo:{
          id:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },
    modifyhostname(params){
      let p = ''
      p += '?username='+params.username
      p += '&password='+params.password
      p += '&port='+params.port
      p += '&ip='+params.ip
      p += '&hostname='+params.hostname
      fetch( process.env.VUE_APP_FETCH+'/web/hostname_set'+p,{method:'get'}).then(res=>{
        return res.json()
      }).then(res=>{
        if(res.data == 'oK'){
          this.$message.info('修改主机名成功')
        }else{
          this.$message.info(res.data)
        }
        let p2 = ''
        p2 += '?username='+params.username
        p2 += '&password='+params.password
        p += '&port='+params.port
        p2 += '&ip='+ip
        fetch( process.env.VUE_APP_FETCH+'/web/init_configure'+p2,{method:'get'}).then(res=>{
          return res.json()
        }).then(res=>{
          if(res.data == 'ok'){
            this.$message.info('执行成功')
          }else{
            this.$message.info('执行失败')
          }
        })
      })
    },
    configokfun(params){
      let p = ''
      p += '?username='+params.username
      p += '&password='+params.password
      p += '&port='+params.port
      let d = this.data.find(item=>{return item.id == params.id})
      let ip = d.ip
      p += '&ip='+ip
      fetch( process.env.VUE_APP_FETCH+'/web/hostname_detect'+p,{method:'get'}).then(res=>{
        return res.json()
      }).then(res=>{
        if(res.data == 'ok'){
          this.$confirm({
            title: '监测到默认主机名，是否需要修改主机名?',
            okText: '是',
            okType: 'danger',
            cancelText: '否',
            onOk:()=> {
              let modalinfo = {
                title:'修改主机名',
                list:[
                  {name:'hostname',label:'主机名',require:true,type:'input',errortip:'请填写主机名'},
                ],
                ok:this.modifyhostname.bind(this),
                moreinfo:{
                  id:params.id,
                  username:params.username,
                  password:params.password,
                  port:params.port,
                  ip:ip
                }
              }
              this.$refs.mymodal.showmodal(modalinfo)
            },
            onCancel:()=>{
              let p2 = ''
              p2 += '?username='+params.username
              p2 += '&password='+params.password
              p += '&port='+params.port
              p2 += '&ip='+ip
              fetch( process.env.VUE_APP_FETCH+'/web/init_configure'+p2,{method:'get'}).then(res=>{
                return res.json()
              }).then(res=>{
                if(res.data == 'ok'){
                  this.$message.info('执行成功')
                }else{
                  this.$message.info('执行失败')
                }
              })
            }
          });
        }else{
          let p2 = ''
          p2 += '?username='+params.username
          p2 += '&password='+params.password
          p += '&port='+params.port
          p2 += '&ip='+ip
          fetch( process.env.VUE_APP_FETCH+'/web/init_configure'+p2,{method:'get'}).then(res=>{
            return res.json()
          }).then(res=>{
            console.log(res)
            if(res.data == 'ok'){
              this.$message.info('执行成功')
            }else{
              this.$message.info('执行失败')
            }
          })
        }
      })



      // setTimeout(()=>{
      //   let modalinfo = {
      //     title:'配置',
      //     list:[
      //       {name:'name',label:'是否立即做可用性检测',require:true,type:'radio',errortip:'请选择是否立即做可用性检测',radiodata:TrueFalse,value:'0'},
      //       {name:'groupId',label:'是否立即做安全检测',require:true,type:'radio',errortip:'请选择是否立即做安全检测',radiodata:TrueFalse,value:'0'},
      //     ],
      //     ok:this.confirmconfigokfun.bind(this),
      //     moreinfo:{
      //       id:params.id
      //     }
      //   }
      //   this.$refs.mymodal.showmodal(modalinfo)
      // },0)

    },
    confirmconfigokfun(params){

    },

    checkable(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      let id = this.getids()
      let hosts = []
      for(let i in this.selectarray){
        hosts.push(this.selectarray[i].name)
      }
      let data = hosts.join(', ')
      let modalinfo = {
        title:'是否立即进行可用性探测',
        list:[
          // {name:'host',label:'选定的主机',type:'show',data:data},
          {name:'port',label:'端口',require:true,type:'number',errortip:'请输入端口'},
          {name:'username',label:'用户名',require:true,type:'input',errortip:'请选择用户名'},
          {name:'password',label:'密码',type:'password',require:true,errortip:'请输入密码'},
        ],
        ok:this.ableok.bind(this),
        tip:'可用性探测是用来验证主机资产所属配置的账号密码是否具备补丁检测与推送的可执行权限',
        moreinfo:{
          id:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },
    ableok(params){
      let p = ''
      p += '?username='+params.username
      p += '&password='+params.password
      p += '&port='+params.port
      let d = this.data.find(item=>{return item.id == params.id})
      let ip = d.ip
      p += '&ip='+ip
      fetch( process.env.VUE_APP_FETCH+'/web/usability_detect'+p,{method:'get'}).then(res=>{
        return res.json()
      }).then(res=>{
        console.log(res)
        if(res.data){
          this.$message.success('执行成功')
        }
      })
    },

    checksafe(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      let id = this.getids()
      let hosts = []
      for(let i in this.selectarray){
        hosts.push(this.selectarray[i].name)
      }
      let data = hosts.join(', ')
      let modalinfo = {
        title:'是否立即进行安全检查',
        list:[
          {name:'host',label:'选定的主机',type:'show',data:data},
        ],
        ok:this.safeok.bind(this),
        tip:'安全检查是远程对主机资产进行评估，并获得被检测主机资产的待修复漏洞数量及详情',
        moreinfo:{
          id:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },
    safeok(params){
      let p = '?id='+params.id
      this.safecheckloading = true
      fetch( process.env.VUE_APP_FETCH+'/web/safe_detect_immediately'+p,{method:'get'}).then(res=>{
        return res.json()
      }).then(res=>{
        this.safecheckloading = false
        if(res.data == 'ok'){
          this.$message.info('操作成功,将在系统后台执行,请在几分钟后查看')
        }else{
          this.$message.info(res.data)
        }
      })
    },
    hebing(){
      if(!this.selectarray || this.selectarray.length <= 1){
        this.$message.info('请先选择至少两条数据')
        return
      }
      let id = this.getids()
      mergeassets({ids:id}).then(res=>{
        this.$message.info('操作成功')
        this.getassetslist()
      })
    },

    autocheckrule(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      let id = this.getids()
      let modalinfo = {
        title:'主机自动安检规则配置',
        list:[
          {name:'type',label:'是否开启自动安检',require:true,type:'radio',errortip:'请选择是否开启自动安检',radiodata:TrueFalse},
          {name:'time',label:'安检周期',require:true,type:'cycle',errortip:'请选择安检周期'},
        ],
        ok:this.autookfun.bind(this),
        moreinfo:{
          id:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },
    autookfun(params){
      if(params.type == 1){
        let p = '?id='+params.id
        p+='&time='+encodeURIComponent(params.time)
        fetch( process.env.VUE_APP_FETCH+'/web/safe_detect_configure'+p,{method:'get'}).then(res=>{
          return res.json()
        }).then(res=>{
          if(res.data == 'ok'){
            this.$message.info('执行成功')
          }else{
            this.$message.info(res.data)
          }
        })
      }else{
        let p = '?id='+params.id
        fetch( process.env.VUE_APP_FETCH+'/web/remove_safe_detect'+p,{method:'get'}).then(res=>{
          return res.json()
        }).then(res=>{
          if(res.data == 'ok'){
            this.$message.success('执行成功')
          }else{
            this.$message.info(res.data)
          }
        })
      }
    },
    importexcel(){
    },
    exportexcel(){
      window.open('/api/back/excel/exportInventorylist')
    },
    resetform(){
      this.form.resetFields()
      this.search = ''
    },
    getassetslist(){
      let params = {}
      params.pageNo = this.tablepagination.current
      params.pageSize = 10
      params.groupId = this.groupId
      if(this.advanced){
        Object.assign(params,this.form.getFieldsValue())
      }else{
        params.search = this.search
      }
      getAssetsinfolist(params).then(res=>{
        let record = res.data.records
        for(let i in record){
          record[i].key =record[i].id
        }
        this.data = record
        this.tablepagination.total = res.data.total

          this.changescale()
      })
    },
    transtotree(list){
      let arry = []
      for(let i in list){
        let json = {}
        json.title = list[i].name
        json.key = list[i].id
        json.value = list[i].id
        if(list[i].childNode){
          json.children = this.transtotree(list[i].childNode)
        }
        arry.push(json)
      }
      return arry
    },
    loaddepartment(){
      getDepartmentList({isHaveWhole:1}).then(res=>{
        if(res.data){
          this.treedataWhole = this.transtotree(res.data)
          this.treedata = this.treedataWhole[0].children
        }
      })
    },
    selecttree(key,e){
      this.groupId = key[0]
      this.getassetslist()
    },
    getids(){
      let arry = []
      for(let i in this.selectarray){
        arry.push(this.selectarray[i].id)
      }
      let id = arry.join(',')
      return id
    },
    deleteassets(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
     let id = this.getids()

      this.$confirm({
        title: '是否确认删除数据?',
        okText: '是',
        okType: 'danger',
        cancelText: '否',
        onOk:()=> {
          deleteassets({id:id}).then(res=>{
            this.$message.success('操作成功')
            this.getassetslist()
          })
        }
      });

    },
    setdepartment(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      let id = this.getids()

      let modalinfo = {
        title:'设置组织',
        list:[
          {name:'groupId',label:'所属部门',require:true,type:'tree',errortip:'请选择部门',treedata:this.treedata},
        ],
        ok:this.setdepartokfun.bind(this),
        moreinfo:{
          inventoryIds:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },

    setdepartokfun(params){
      setassetsdepartment(params).then(res=>{
        this.$message.success('操作成功')
        this.getassetslist()
      })
    },
    settestfun(params){
      if(params.isTest == '1'){
        let content = ''
        let ids = params.ids.split(',')
        for(let i in ids){
          let data = this.data.find(item=>{
            return item.id == ids[i]
          })
          content += (data.name + (data.ip?('-'+data.ip):'')+'\r\n')
        }
        this.$confirm({
          title: '是否将下列选中的主机资产设置为测试主机',
          content: content,
          class:'pre',
          onOk:()=> {
            this.$confirm({
              title: '温馨提示',
              content: '主机资产标记为测试后,在启动“测试补丁分发策略”后将收到未经管理员确认的补丁文件，请确认是否已识别相关风险',
              class:'pre',
              onOk:()=> {
                setassetsTest(params).then(res=>{
                  this.$message.success('操作成功')
                  this.getassetslist()
                })
              },
              onCancel() {},
            });
          },
          onCancel() {},
        });
      }else{
        setassetsTest(params).then(res=>{
          this.$message.success('操作成功')
          this.getassetslist()
        })
      }
    },

    settest(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      let id = this.getids()

      let modalinfo = {
        title:'设置测试',
        list:[
          {name:'isTest',label:'是否测试主机',type:'select',selectdata:this.TestMark},
        ],
        ok:this.settestfun.bind(this),
        moreinfo:{
          ids:id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },
    modifyassets(){
      if(!this.selectarray || this.selectarray.length == 0){
        this.$message.info('请先选择一条数据')
        return
      }
      if(this.selectarray && this.selectarray.length >1){
        this.$message.info('此操作只支持一条数据')
        return
      }

      let item = this.selectarray[0]

      let modalinfo = {
        title:'修改资产信息',
        list:[
          // {name:'id',label:'资产ID',require:true,type:'number',errortip:'请输入资产ID',value:item.id,disable:true},
          {name:'name',label:'资产名称',require:true,type:'input',errortip:'请输入名称',value:item.name},
          {name:'groupId',label:'资产所属部门',require:true,type:'tree',errortip:'请选择部门',treedata:this.treedata,value:item.groupId},
          {name:'ip',label:'IP地址',type:'ip',value:item.ip,require:true,errortip:'请输入IP地址'},
          {name:'owner',label:'资产所属人',type:'input',value:item.owner},
        ],
        ok:this.createokfun.bind(this),
        moreinfo:{
          id:item.id
        }
      }
      this.$refs.mymodal.showmodal(modalinfo)

    },

    createnew(){
      let modalinfo = {
        title:'新建资产信息',
        list:[
          {name:'name',label:'资产名称',require:true,type:'input',errortip:'请输入名称'},
          {name:'groupId',label:'资产所属部门',require:true,type:'tree',errortip:'请选择部门',treedata:this.treedata},
          {name:'ip',label:'IP地址',type:'ip',require:true,errortip:'请输入IP地址'},
          {name:'owner',label:'资产所属人',type:'input'},
        ],
        ok:this.createokfun.bind(this),
      }
      this.$refs.mymodal.showmodal(modalinfo)
    },

    createokfun(params){
      addassets(params).then(res=>{
        this.$message.success('操作成功')
        this.getassetslist()
      })
    },



    showDrawer() {
      this.visible = true
    },
    onClose() {
      this.visible = false
    },
    onDateChange(date, dateString) {
      console.log(date, dateString)
    },

    getParentKey(key, tree){
      let parentKey;
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i];
        if (node.children) {
          if (node.children.some(item => item.key === key)) {
            parentKey = node.key;
          } else if (this.getParentKey(key, node.children)) {
            parentKey = this.getParentKey(key, node.children);
          }
        }
      }
      return parentKey;
    },
    getallnode(list){
      let array = []
      for(let i =0;i<list.length;i++){
        array.push(list[i])
        if(list[i].children){
          array = array.concat(this.getallnode(list[i].children))
        }
      }
      return array
    },
    onSearch(value) {
      let list = this.getallnode(this.treedata)
      let expandedKeys1 = list
        .map(item => {
          console.log(item)
          if (item.title.indexOf(value) > -1) {
            return this.getParentKey(item.key, this.treedata);
          }
          return null;
        })
      let expandedKeys = expandedKeys1
        .filter((item, i, self) => item && self.indexOf(item) === i);
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true,
      });
    },
    getpack(record){
      let modalinfo = {
        title:'已安装包详情',
        type:'table',
        query:getpackinfo,
        params:{hostname:record.hostname},
        width:1000,
        list:[
          {label:'软件名称',value:'name'},
          {label:'所属系统架构',value:'arch'},
          {label:'主机名',value:'hostname'},
          {label:'最新版本号',value:'newReleaseNo'},
          {label:'发行包所属系统版本',value:'releaseNo'},
          {label:'所属仓库',value:'repository'},
          {label:'软件版本',value:'version'},
        ],
      }
      this.$refs.detailmodal.showmodal(modalinfo)
    },
    gethole(record){
      let modalinfo = {
        title:'漏洞详情',
        type:'table',
        width:1000,
        query:getholeinfo,
        search:true,
        params:{host:record.ip},
        list:[
          {label:'影响的软件',value:'affectPackages',  width:200},
          {label:'漏洞ID',value:'cveId'},
          {label:'安全评分',value:'cveCvss3Score'},
          {label:'危险等级',value:'cveCvss3Severity'},
          {label:'补丁版本',value:'patchNo'},
          {label:'更新时间',value:'utime'},

        ],
      }
      this.$refs.detailmodal.showmodal(modalinfo)
    },
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
      this.changescale()
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    toggleAdvanced() {
      this.advanced = !this.advanced

      this.changescale()
    },
    changescale(){
      let n = document.createElement('div')
      n.id = 'abc'
      n.setAttribute('style',' position: fixed;z-index: 999;left: 0;top:0;width: 100%;height: 100%;background-color: #fff')
      document.body.append(n)
      document.getElementById('app').style.transform = `scale(1)`
      document.getElementById('app').style.transformOrigin = `0 0`
      document.getElementById('app').style.height = `100%`
      setTimeout(()=>{
        document.getElementById('abc').remove()
        var scale = (window.innerWidth-20)/1900
        document.getElementById('app').style.transform = `scale(${scale})`
        document.getElementById('app').style.transformOrigin = `0 0`
        document.getElementById('app').style.height = `${100/scale}%`
      },0)
    }
  },
}
</script>

<style lang="less" scoped>
.ant-pro-components-tag-select {
  /deep/ .ant-pro-tag-select .ant-tag {
    margin-right: 24px;
    padding: 0 8px;
    font-size: 14px;
  }
}
</style>
<style>
  .pre{
    white-space: pre-line;
  }
</style>